package com.ohos.carbon.slice.widgets;

import com.ohos.carbon.ResourceTable;
import com.ohos.carbon.model.DayBean;
import com.ohos.carbon.provider.Drop_Down_DayProvider;
import com.ohos.carbon.provider.Drop_Year_MonthProvider;
import com.ohos.carbon.LogUtils;
import com.ohos.carbon.slice.MainAbilitySlice;
import ohos.aafwk.ability.AbilitySlice;
import ohos.aafwk.content.Intent;
import ohos.agp.components.*;
import ohos.agp.components.element.Element;
import ohos.agp.components.element.ElementScatter;

import java.util.*;

import static com.ohos.carbon.slice.MainAbilitySlice.pf;
public class DropDownSlice extends AbilitySlice {

    private static final String TAG = "DropDownSlice";
    private Image back_button, day_image, month_image, year_image;
    private Text text_title, text_month, text_value;
    private DirectionalLayout day_lin, month_lin, year_lin, day_direction,
            month_direction, drap_down_layout;
    private DependentLayout year_image_layout, textfiled_drop;
    private Component textfiled_view;
    private ListContainer listContainer_year, listContainer_month, listContainer_day;
    private List<String> mList_year;
    private List<String> mList_month;
    private List<DayBean> mList_day;
    private TextField textField;
    private boolean flag = true;
    private Map<Integer, Integer> mMap;
    private String mStr;
    private String str ="";
    private String s;
    private Drop_Down_DayProvider dayProvider;

    @Override
    public void onStart(Intent intent) {
        super.onStart(intent);
        super.setUIContent(ResourceTable.Layout_ability_drop_down);
        initData();
        initView();
        initClick();

        MainAbilitySlice.all = (Map<String, Integer>) pf.getAll();
        if (MainAbilitySlice.all.size() != 0){
            if (MainAbilitySlice.all.containsKey("DropDown")) {
                for (Map.Entry<String, Integer> entry : MainAbilitySlice.all.entrySet()) {
                    if (entry.getKey().equals("DropDown")) {
                        int integer = entry.getValue();
                        integer++;
                        pf.putInt("DropDown", integer);
                        pf.flushSync();
                    }
                }
            }else {
                pf.putInt("DropDown", 1);
                pf.flushSync();
            }
        }else {
            pf.putInt("DropDown", 1);
            pf.flushSync();
        }
    }


    private void initData() {
        initYear();
        initMonth();
        initDay();
    }

    private void initDay() {
        mList_day = new ArrayList<>();
        for (int i = 1; i < 32; i++) {
            mList_day.add(new DayBean(ResourceTable.Media_checkbox_false1, i + ""));
        }
    }

    private void initMonth() {
        mList_month = new ArrayList<>();
        mList_month.add("Jan");
        mList_month.add("Feb");
        mList_month.add("Mar");
        mList_month.add("Apr");
        mList_month.add("May");
        mList_month.add("Jun");
        mList_month.add("Jul");
        mList_month.add("Aug");
        mList_month.add("Sep");
        mList_month.add("Oct");
        mList_month.add("Nov");
        mList_month.add("Dec");
    }

    private void initYear() {
        mList_year = new ArrayList<>();
        for (int i = 1987; i < 2017; i++) {
            mList_year.add(i + "");
        }
    }

    private void initView() {
        LogUtils.d(TAG, str);
        back_button = (Image) findComponentById(ResourceTable.Id_back_button);
        text_title = (Text) findComponentById(ResourceTable.Id_title_text);
        day_image = (Image) findComponentById(ResourceTable.Id_day_image);
        month_image = (Image) findComponentById(ResourceTable.Id_month_image);
        year_image = (Image) findComponentById(ResourceTable.Id_year_image);
        textfiled_view = findComponentById(ResourceTable.Id_textfiled_view);
        text_month = (Text) findComponentById(ResourceTable.Id_text_month);
        listContainer_year = (ListContainer) findComponentById(ResourceTable.Id_year_list);
        listContainer_month = (ListContainer) findComponentById(ResourceTable.Id_month_list);
        textField = (TextField) findComponentById(ResourceTable.Id_textfiled);
        text_value = (Text) findComponentById(ResourceTable.Id_text_value);
        textfiled_drop = (DependentLayout) findComponentById(ResourceTable.Id_textfiled_drop);
        year_image_layout = (DependentLayout) findComponentById(ResourceTable.Id_year_image_layout);
        drap_down_layout = (DirectionalLayout) findComponentById(ResourceTable.Id_drop_down_layout);
        day_direction = (DirectionalLayout) findComponentById(ResourceTable.Id_day_direction);
        listContainer_day = (ListContainer) findComponentById(ResourceTable.Id_day_list);
        day_lin = (DirectionalLayout) findComponentById(ResourceTable.Id_day_lin);
        month_direction = (DirectionalLayout) findComponentById(ResourceTable.Id_month_direction);
        month_lin = (DirectionalLayout) findComponentById(ResourceTable.Id_month_lin);
        year_lin = (DirectionalLayout) findComponentById(ResourceTable.Id_year_lin);
        text_title.setText("DropDown");
        back_button.setImageElement(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_uparrow));
        mMap = new HashMap<>();
        for (int i = 1; i < 32; i++) {
            mMap.put(i, 0);
        }
        listContainer_year.setItemProvider(new Drop_Year_MonthProvider(mList_year, getContext()));
        listContainer_year.setItemClickedListener((listContainer, component, i, l) -> {
            String s = mList_year.get(i);
            textField.setText(s);
            year_lin.setVisibility(Component.HIDE);
        });
        listContainer_month.setItemProvider(new Drop_Year_MonthProvider(mList_month, getContext()));
        listContainer_month.setItemClickedListener((listContainer, component, i, l) -> {
            text_month.setText(mList_month.get(i));
            month_lin.setVisibility(Component.HIDE);
        });
        dayProvider = new Drop_Down_DayProvider(mList_day, getContext());
        listContainer_day.setItemProvider(dayProvider);
        listContainer_day.setItemClickedListener(new ListContainer.ItemClickedListener() {
            @Override
            public void onItemClicked(ListContainer listContainer, Component component, int i, long l) {
                LogUtils.d(TAG,"onItemClicked");
                int icon = mList_day.get(i).getIcon();
                if (icon == ResourceTable.Media_checkbox_false1){
                    mMap.put(i + 1,1);
                    mList_day.get(i).setIcon(ResourceTable.Media_checkbox_true_yellow);
                    dayProvider.notifyDataChanged();
                } else {
                    mMap.put(i + 1,0);
                    mList_day.get(i).setIcon(ResourceTable.Media_checkbox_false1);
                    dayProvider.notifyDataChanged();
                }

            }
        });
        back_button.setClickedListener(component -> onBackPressed());
    }

    private void initClick() {
        drap_down_layout.setClickedListener(component -> {
            day_lin.setVisibility(Component.HIDE);
            month_lin.setVisibility(Component.HIDE);
            year_lin.setVisibility(Component.HIDE);
            textfiled_view.setBackground(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_textfiled_dropdown_false));
            Iterator<Map.Entry<Integer, Integer>> iterator = mMap.entrySet().iterator();
            while (iterator.hasNext()) {
                Map.Entry<Integer, Integer> entry = iterator.next();
                Integer key = entry.getKey();
                Integer value = entry.getValue();
                if (value == 1) {
                    mStr = String.valueOf(key);
                    if (str.indexOf(mStr) == -1){
                        str += mStr + ",";
                    }
                }else {
                    mStr = String.valueOf(key);
                    if (str.indexOf(mStr) != -1){
                        int index = str.indexOf(mStr);
                        str = str.substring(0,index) + str.substring(index + 1);
                    }
                }
            }
            if (str.equals(",")){
                str = "";
            }
             text_value.setText(str);

        });
        Element element = ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_carbon_dropdown);
        day_image.setImageElement(element);
        month_image.setImageElement(element);
        year_image.setImageElement(element);
        day_direction.setClickedListener(component -> {
            month_lin.setVisibility(Component.HIDE);
            year_lin.setVisibility(Component.HIDE);
            day_lin.setVisibility(Component.VISIBLE);
        });
        month_direction.setClickedListener(component -> {
            day_lin.setVisibility(Component.HIDE);
            year_lin.setVisibility(Component.HIDE);
            month_lin.setVisibility(Component.VISIBLE);
        });
        year_image_layout.setClickedListener(component -> {
            if (flag) {
                if (textField.getText().equals("1987")) {
                    textfiled_view.setBackground(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_textfiled_dropdown_true));
                    year_lin.setVisibility(Component.VISIBLE);
                    month_lin.setVisibility(Component.HIDE);
                    day_lin.setVisibility(Component.HIDE);
                    flag = !flag;
                } else {
                    textfiled_view.setBackground(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_textfiled_dropdown_true));
                    year_lin.setVisibility(Component.VISIBLE);
                    month_lin.setVisibility(Component.HIDE);
                    day_lin.setVisibility(Component.HIDE);
                    String text = textField.getText();
                    mList_year.add(0, text);
                    flag = !flag;
                }
            } else {
                textfiled_view.setBackground(ElementScatter.getInstance(getContext()).parse(ResourceTable.Graphic_textfiled_dropdown_true));
                year_lin.setVisibility(Component.VISIBLE);
                month_lin.setVisibility(Component.HIDE);
                day_lin.setVisibility(Component.HIDE);
                String text = textField.getText();
                mList_year.remove(0);
                mList_year.add(0, text);
            }
        });
    }

    @Override
    public void onActive() {
        super.onActive();
    }

    @Override
    public void onForeground(Intent intent) {
        super.onForeground(intent);
    }
}
